package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.mall.dto.ItemDto;
import com.cskaoyan.mall.dto.ProductDetailDto;
import com.cskaoyan.mall.dto.ProductDto;
import com.cskaoyan.shopping.converter.*;
import com.cskaoyan.shopping.dal.entitys.*;
import com.cskaoyan.shopping.dal.persistence.*;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.form.PageRequest;
import com.cskaoyan.shopping.service.IProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class IProductServiceImpl implements IProductService {

    @Autowired
    PanelMapper panelMapper;
    @Autowired
    PanelContentMapper panelContentMapper;
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    PanelConverter panelConverter;
    @Autowired
    ItemCatMapper itemCatMapper;
    @Autowired
    ItemCatConverter itemCatConverter;
    @Autowired
    ItemDescMapper itemDescMapper;
    @Autowired
    ItemConverter itemConverter;

    @Autowired
    ProductConverter productConverter;
    @Autowired
    ContentConverter contentConverter;


    @Override
    public List<Panel> shoppingHomepage() {
        // 1.查询panel中的数据
        List<Panel> panels = panelMapper.selectAll();
        if (panels == null) {
            throw new BizException(SysRetCodeConstants.DATA_NOT_EXIST.getCode(), SysRetCodeConstants.DATA_NOT_EXIST.getMessage());
        }
        for (Panel panel : panels) {
            List<PanelContentItem> panelContentItems = new ArrayList<>();
            // 2.查询panel_content中的数据
            Example example = new Example(PanelContent.class);
            example.createCriteria().andEqualTo("panelId", panel.getId());
            List<PanelContent> panelContents = panelContentMapper.selectByExample(example);
            if (panelContents == null) {
                throw new BizException(SysRetCodeConstants.DATA_NOT_EXIST.getCode(), SysRetCodeConstants.DATA_NOT_EXIST.getMessage());
            }
            for (PanelContent panelContent : panelContents) {
                // 3.查询商品中的数据
                Item item = itemMapper.selectByPrimaryKey(panelContent.getProductId());
                PanelContentItem panelContentItem = panelConverter.panelContentItems(panelContent, item);
                panelContentItems.add(panelContentItem);
            }
            panel.setPanelContentItems(panelContentItems);
        }
        return panels;
    }

    @Override
    public List<PanelContent> shoppingNavigation() {
        // 在panel_content表中查询panelId为0的panel信息
        Example example = new Example(PanelContent.class);
        example.createCriteria().andEqualTo("panelId", 0);
        List<PanelContent> panelContents = panelContentMapper.selectByExample(example);
        return panelContents;
    }

    @Override
    public List<ItemCatDto> shoppingCategories() {
//        List<ItemCat> itemCats = itemCatMapper.selectAll();
//        List<ItemCat> itemCats = itemCatMapper.selectAllBySortOrder();
        // 按sort_order升序查询
        Example example = new Example(ItemCat.class);
        example.setOrderByClause("sort_order asc");
        List<ItemCat> itemCats = itemCatMapper.selectByExample(example);
        List<ItemCatDto> itemCatDto = itemCatConverter.itemCatsToDtos(itemCats);
        return itemCatDto;
    }

    @Override
    public ProductDetailDto getProductDetail(Long id) {
        // 1.查询item表获取商品信息
        Item item = itemMapper.selectByPrimaryKey(id);
        // 2.查询item_decs表获取商品详情
        ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(id);
        // 封装
        ProductDetailDto productDetailDto = itemConverter.itemToProductDetailDto(item, itemDesc);
        return productDetailDto;
    }


    @Override
    public AllProductResponse getAllProduct(PageRequest pageInfo) {
        //sort为1：价格从低到高
        //sort为-1：价格从高到低
        //sort为空：默认排序

        //首先判断


        AllProductResponse allProductResponse = new AllProductResponse();

        //1.通过example对象拿到list<Item>
        Example itemExample = new Example(Item.class);
//        itemExample.createCriteria().andEqualTo("cid", pageInfo.getCid()).andGreaterThan("price", pageInfo.getPriceGt()).andLessThan("price", pageInfo.getPriceLte());

        //分页
        PageHelper.startPage(pageInfo.getPage(), pageInfo.getSize());

        //判断是否为升序
        if ("1".equals(pageInfo.getSort())) {
            itemExample.setOrderByClause("price ASC");
        }
        //判断是否为降序
        if ("-1".equals(pageInfo.getSort())) {
            itemExample.setOrderByClause("price DESC");
        }

        if (!("1".equals(pageInfo.getSort())) && !("-1".equals(pageInfo.getSort()))) {
            //否则通过创建时间降序排序
            itemExample.setOrderByClause("created DESC");
        }

//        price区间查询
        Example.Criteria criteria = itemExample.createCriteria();
        if(pageInfo.getPriceGt()!= null){
            criteria.andLessThanOrEqualTo("price",pageInfo.getPriceLte());
        }
        if(pageInfo.getPriceLte()!= null){
            criteria.andGreaterThanOrEqualTo("price",pageInfo.getPriceGt());
        }
        if(pageInfo.getCid()!= null){
            criteria.andEqualTo("cid",pageInfo.getCid());
        }


        List<Item> items = itemMapper.selectByExample(itemExample);

        //2.转换对象
        List<ProductDto> productDtos = productConverter.items2Dto(items);


//        Example example = new Example(Item.class);
//        example.createCriteria().andEqualTo("cid", pageInfo.getCid());
//        int totals = itemMapper.selectCountByExample(example);


        PageInfo<Item> itemPageInfo = PageInfo.of(items);

        Long total = itemPageInfo.getTotal();
        allProductResponse.setTotal(total);
        allProductResponse.setData(productDtos);


        return allProductResponse;
    }

    @Override
    public Set<PanelDto> getRecommendGoods() {

        //1.通过panelId先查询到tb_panel表中热门的热门推介
        Panel panel = panelMapper.selectByPrimaryKey(6);

        //对象转换

        PanelDto panelDto = contentConverter.panel2Dto(panel);


        //2.通过panelId查询tb_panel_content
        List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(panelDto.getId());

        //对象类型转换  List<PanelContentItem>  ---- >  list<PanelContentItemDto>
        List<PanelContentItemDto> panelContentItemDtos = contentConverter.panelContentItem2Dto(panelContentItems);

        panelDto.setPanelContentItems(panelContentItemDtos);

        Set<PanelDto> panelDtos = new HashSet();
        panelDtos.add(panelDto);
        return panelDtos;
    }

    @Override
    public List<ItemDto> getAllItems() {
        return null;
    }
}
